package deepbrain.simnetwork.loader.matrix;

import java.util.HashSet;
import java.util.Set;

import deepbrain.simnetwork.structure.Connection;
import deepbrain.simnetwork.structure.ConnectionMatrix;

/**
 * 
 * 
 * @author Li Yang
 * @create 2009-2-25
 */
public class GenericMatrixConnectionBuilder<WeightType> implements
		MatrixConnectionBuilder<WeightType> {

	public Connection<WeightType> buildConnection(WeightType weight,
			int source, int destination) {
		return new Connection<WeightType>(source, destination, weight);
	}

	@SuppressWarnings("unchecked")
	public Set<Connection<?>> buildConnections(
			ConnectionMatrix<WeightType> connectionMatrix) {
		Set<Connection<?>> connections = new HashSet<Connection<?>>();
		Object[][] matrix = connectionMatrix.getMatrix();
		for (int y = 0; y < connectionMatrix.getNumOfRows(); y++) {
			for (int x = 0; x < connectionMatrix.getNumOfCols(); x++) {
				if (matrix[y][x] != null) {
					Connection<WeightType> connection = buildConnection(
							(WeightType)matrix[y][x], y, x);
					if (connection != null)
						connections.add(connection);
				}
			}
		}
		return connections;
	}

}
